﻿

using Elasticsearch.Net;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using Warehouse.Domain.DTO;
using Warehouse.Infrstructre.Interface;
using Warehouse.Read.API.Applcation.Command.EnterStoragesCommand;
using WMSpublic;
using static WMSpublic.ParseAPI;
using Audit = WMSpublic.Audit;

namespace Warehouse.Read.API.Applcation.CommandHandlers.EnterStoragesCommandHandler
{
    public class EnterStoragesSelectCommandHandler : IRequestHandler<EnterStoragesSelectCommand, EnterStoragesSelectDtos>
    {
        private readonly IEnterStoragesInfoRepsitory infoRep;
        private readonly IEnterStoragesRepsitory entRep;

        public EnterStoragesSelectCommandHandler(IEnterStoragesInfoRepsitory infoRep, IEnterStoragesRepsitory entRep)
        {
            this.infoRep = infoRep;
            this.entRep = entRep;
        }

        /// <summary>
        /// 显示入库单列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<EnterStoragesSelectDtos> Handle(EnterStoragesSelectCommand request, CancellationToken cancellationToken)
        {
            List<Products> products = await GetProductsInfo(1, 1000);
            List<StoreHouse> houses = await GetStoreInfo(1, 1000);
            List<ProductType> type = await GetproductType(1, 1000);

            var list = (from a in await entRep.GetAllAsyns()
                        join b in await infoRep.GetAllAsyns() on a.StockId equals b.StockId
                        join c in products on b.ProductsID equals c.ProductsID
                        join d in houses on b.Binlocation equals d.StorehouseId
                        join e in type on c.ProductTypeID equals e.ProductTypeID
                        where a.IsDel == false && a.office_id == request.office_id
                        select new newEnterStoragesSelectDtos
                        {
                            StockId = a.StockId,
                            StockCode = a.StockCode,
                            StockOrderType = a.StockOrderType,
                            VendorName = a.VendorName,
                            MakePerson = a.MakePerson,
                            CreateTime = (DateTime)a.AddTime,
                            StockProductId = b.StockProductId,
                            Batch = b.Batch,
                            Quantity = b.Quantity,
                            StateSP = b.StateSP,
                            ShenHePeople = b.ShenHePeople,
                            ShenHeTime = b.ShenHeTime,

                            ProductsID = c.ProductsID,
                            ProductsName = c.ProductsName,
                            ProductsCode = c.ProductsCode,
                            Specs = c.Specs,

                            StorehouseName = d.StorehouseName,

                            Sprice = b.Sprice,
                            ProductTypeName = e.ProductTypeName,
                            Binlocation = b.Binlocation
                        }).DistinctBy(x => x.StockId);
            if (request.StockCode != null)
            {
                list = list.Where(x => x.StockCode.Contains(request.StockCode));
            }
            if (request.StateSP != null)
            {
                list = list.Where(x => x.StateSP == request.StateSP);
            }
            if (request.StockOrderType != null)
            {
                list = list.Where(x => x.StockOrderType == request.StockOrderType);
            }
            if (request.ProductsCode != null)
            {
                list = list.Where(x => x.ProductsCode.Contains(request.ProductsCode));
            }
            if (request.ProductsName != null)
            {
                list = list.Where(x => x.ProductsName.Contains(request.ProductsName));
            }
            EnterStoragesSelectDtos dto = new EnterStoragesSelectDtos();
            dto.pagecount = list.Count();
            dto.totalcount = (int)Math.Ceiling(dto.pagecount * 1.0 / request.PageSize);
            dto.list = list.OrderByDescending(x => x.CreateTime).ToList();
            return dto;
        }
        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetProductsInfo(int pageindex, int pagesize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyNCIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMSIsImp0aSI6IjQzMzA2NmRmLWYyNjAtNDI2Ny1iYWY5LWJkMzgxZGVhZGMwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiLmnLHms4rpnJYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy91cmkiOiIiLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.E0PG5CbU-_2efHW14aTC5dmRc3FC7BLU0TIgjNsA1wo";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://116.205.121.197:8000/cpd/api/ProductRead/GetAllProductInfo?pageindex=" + pageindex + "&pagesize=" + pagesize;
            string postRep = await client.GetAsync(url);
            var vendors = JsonConvert.DeserializeObject<ApiResultList<Products>>(postRep);
            return vendors.data.list;
        }
        /// <summary>
        /// 获取库位信息
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetStoreInfo(int pageindex, int pagesize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyNCIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMSIsImp0aSI6IjQzMzA2NmRmLWYyNjAtNDI2Ny1iYWY5LWJkMzgxZGVhZGMwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiLmnLHms4rpnJYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy91cmkiOiIiLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.E0PG5CbU-_2efHW14aTC5dmRc3FC7BLU0TIgjNsA1wo";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://116.205.121.197:8000/ckd/api/ReadStorehouse/GetStorehouseList?pageIndex=" + pageindex + "&PageSize=" + pagesize;
            string postRep = await client.GetAsync(url);
            var vendors = JsonConvert.DeserializeObject<ApiResultList<StoreHouse>>(postRep);
            return vendors.data.list;
        }


        /// <summary>
        /// 获取产品类别
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetproductType(int pageindex, int pagesize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyNCIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMSIsImp0aSI6IjQzMzA2NmRmLWYyNjAtNDI2Ny1iYWY5LWJkMzgxZGVhZGMwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiLmnLHms4rpnJYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy91cmkiOiIiLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.E0PG5CbU-_2efHW14aTC5dmRc3FC7BLU0TIgjNsA1wo";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://116.205.121.197:8000/cpd/api/ProductRead/GetAllProduct?pageindex=" + pageindex + "&pagesize=" + pagesize;
            string postRep = await client.GetAsync(url);
            var vendors = JsonConvert.DeserializeObject<ApiResultList<ProductType>>(postRep);
            return vendors.data.list;
        }
        /// <summary>
        /// 产品管理
        /// </summary>
        public class Products : Audit
        {
            /// <summary>
            /// 产品id
            /// </summary>
            [Key]
            public int ProductsID { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            public string? ProductsCode { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            public string? ProductsName { get; set; }
            /// <summary>
            /// 厂商编码
            /// </summary>
            public string? VendorsCode { get; set; }
            /// <summary>
            /// 内部编码
            /// </summary>
            public string? InteriorCode { get; set; }
            /// <summary>
            /// 规格
            /// </summary>
            public string? Specs { get; set; }
            /// <summary>
            /// 产品类别
            /// </summary>
            public int? ProductTypeID { get; set; }
            /// <summary>
            /// 存储单位
            /// </summary>
            public string? StorageUnits { get; set; }
            /// <summary>
            /// 预警（上）
            /// </summary>
            public int? Upperlimit { get; set; }
            /// <summary>
            /// 预警（下）
            /// </summary>
            public int? Lowerlimit { get; set; }
            /// <summary>
            /// 包装类型
            /// </summary>
            public int? WrapType { get; set; }
            /// <summary>
            /// 价格
            /// </summary>
            public decimal? Price { get; set; }
            /// <summary>
            /// 重量
            /// </summary>
            public decimal? Weight { get; set; }
            /// <summary>
            /// 默认仓库
            /// </summary>
            public int? WarehouseId { get; set; }
            /// <summary>
            /// 库位
            /// </summary>
            public int? StorehouseId { get; set; }
            /// <summary>
            /// 默认供应商
            /// </summary>
            public int? VendorId { get; set; }
            /// <summary>
            /// 默认客户
            /// </summary>
            public int? ClientId { get; set; }
            /// <summary>
            /// 描述
            /// </summary>
            public string? Describe { get; set; }
            /// <summary>
            /// 产品数量
            /// </summary>
            public int? ProcurementNum { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? Office_Id { get; set; }

            /// <summary>
            /// 用户id
            /// </summary>
            public int? User_Id { get; set; }
        }
        /// <summary>
        /// 库位表
        /// </summary>
        public class StoreHouse : Audit
        {
            /// <summary>
            /// 主键id
            /// </summary>
            [Key]
            public int StorehouseId { get; set; }
            /// <summary>
            /// 库位编号
            /// </summary>
            public string? StorehouseCode { get; set; }
            /// <summary>
            /// 库位名称
            /// </summary>
            public string? StorehouseName { get; set; }
            /// <summary>
            /// 库位类型
            /// </summary>
            public string? StorehouseType { get; set; }
            /// <summary>
            /// 仓库id
            /// </summary>
            public int? WarehouseId { get; set; }
            /// <summary>
            /// 是否禁用
            /// </summary>
            public bool? IsDisable { get; set; }
            /// <summary>
            /// 是否默认
            /// </summary>
            public bool? IsDefaults { get; set; }
            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime? CreatTime { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string? Sremark { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? office_id { get; set; }
            /// <summary>
            /// 用户id
            /// </summary>
            public int? user_id { get; set; }

        }
        /// <summary>
        /// 产品类别
        /// </summary>
        public class ProductType : Audit
        {
            /// <summary>
            /// 产品类别id
            /// </summary>
            [Key]
            public int ProductTypeID { get; set; }
            /// <summary>
            /// 类别编号
            /// </summary>
            public string? ProductTypeCode { get; set; }
            /// <summary>
            /// 类别名称
            /// </summary>
            public string? ProductTypeName { get; set; }
            /// <summary>
            /// 上级类别
            /// </summary>
            public int? ParentId { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string? Premark { get; set; }
            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime? CreateTime { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? Office_Id { get; set; }

            /// <summary>
            /// 用户id
            /// </summary>
            public int? User_Id { get; set; }
        }
    }
}
